<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Common\ORG\Page,Common\ORG\AttUpload,Common\ORG\Excel;
// Knowledgeproperty manager
class knowledgepropertyController extends BaseController {

	public function index(){
		$wheres = array();
		$wheres['_string'] = '1=1';
		$state = I('post.state', -1,'intval');
		$psize = I('post.psize', $this->m_psize,'intval');
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');

		$knowledge_spot_id = I('post.knowledge_spot_id',false,'intval');
		if($knowledge_spot_id){
			$wheres['kl.knowledge_spot_id'] = $knowledge_spot_id;
			$wheres['kl.is_delete'] = array('neq', 1);
			if( !$psize )
				$psize = $this->m_psize;
			else if( $psize > 20 )
				$psize = $this->m_psize;				
			$cnt = D('Admin/Knowledge')
										->alias("kl")
										->field('kl.*,b.sector_name')
										->join('LEFT JOIN  user as a ON a.user_id = kl.author_id')
										->join('LEFT JOIN  sector as b ON b.sector_id = a.sector_id')
										->where($wheres)
										->count();
			$Page = new \Common\ORG\Page( $cnt, $psize );		
			$dlist = D('Admin/Knowledge')
										->alias("kl")
										->field('kl.*,b.sector_name')
										->join('LEFT JOIN  user as a ON a.user_id = kl.author_id')
										->join('LEFT JOIN  sector as b ON b.sector_id = a.sector_id')
										->where($wheres)
										->order('kl.create_time desc')
										->limit($Page->firstRow.','.$Page->listRows )
										->select();
			if($knowledge_spot_id){
				if($this->m_is_admin){//is_admin
					foreach($dlist as $key =>$val){
						$dlist[$key]['is_hide'] = 0;
						$dlist[$key]['is_delete'] = 0;
					}
					$res['is_hide'] = 1;
				}else{
					// sector publish right
					$data = M('sector')->select();
					$callfunsec = GetParentSector($data,$this->m_user['sector_id']);
					$callfunall = M('knowledge_spot_sector_right')->distinct(true)->field('knowledge_spot_id')->where(array('sector_id'=>array('IN',$callfunsec),'knowledge_spot_id'=>$knowledge_spot_id,'right_type'=>2))->count();
					if($callfunall){
						// if it is level one admin
						$callfuncode = M('knowledge_spot')->field('code')->where(array('knowledge_spot_id'=>$knowledge_spot_id))->find();
						$callfun = M('knowledge_spot')->where(array('code'=>substr($callfuncode['code'],0,2),'level_one_manager'=>$this->m_user['user_id']))->count();
						if($callfun){
						// if($callfun){
							foreach($dlist as $key => $val){
								$dlist[$key]['is_hide'] = 0;
								$dlist[$key]['is_delete'] = 0;
							}
						}else{
							foreach($dlist as $key => $val){
								if($val['author_id'] == $this->m_user['user_id']){
									$dlist[$key]['is_hide'] = 0;
									$dlist[$key]['is_delete'] = 1;
								}else{
									$dlist[$key]['is_hide'] = 1;
									$dlist[$key]['is_delete'] = 1;
								}
							}
						}
						$res['is_hide'] = 1;
					}else{
						// if sector doesn't have publish right ,then check user publish right
						$callfunuser = M('knowledge_spot_user_right')->where(array('user_id'=>$this->m_user['user_id'],'knowledge_spot_id'=>$knowledge_spot_id,'right_type'=>2))->count();
						if($callfunuser){
							$callfuncode = M('knowledge_spot')->field('code')->where(array('knowledge_spot_id'=>$knowledge_spot_id))->find();
							$callfun = M('knowledge_spot')->where(array('code'=>substr($callfuncode['code'],0,2),'level_one_manager'=>$this->m_user['user_id']))->count();
							if($callfun){
								foreach($dlist as $key => $val){
									$dlist[$key]['is_hide'] = 0;
									$dlist[$key]['is_delete'] = 0;
								}
							}else{
								foreach($dlist as $key => $val){
									if($val['author_id'] == $this->m_user['user_id']){
										$dlist[$key]['is_hide'] = 0;
										$dlist[$key]['is_delete'] = 1;
									}else{
										$dlist[$key]['is_hide'] = 1;
										$dlist[$key]['is_delete'] = 1;
									}
								}
							}
							$res['is_hide'] = 1;	
						}else{
							foreach($dlist as $key =>$val){
								$dlist[$key]['is_hide'] = 1;
								$dlist[$key]['is_delete'] = 1;
							}
							$res['is_hide'] = 2;
						}
					}				
				}

			}
			if($dlist){
				foreach($dlist as $key => $value){
					if($value['msg_type'] == 2){
						$knowledgefile= M('knowledge_file')->where(array('knowledge_id'=>$value['knowledge_id']))->find();
						$dlist[$key]['name'] = $knowledgefile['file_original_name'];
						$dlist[$key]['id'] = $knowledgefile['knowledge_file_id'];
					}else{
						$dlist[$key]['name'] = 0;
						$dlist[$key]['id'] = 0;
					}
				}	
			}

			if($dlist){				
				$res['rcode'] = 1;
				$res['is_picture'] = 2;
				$res['data'] = $dlist;
				$res['pall'] =  $Page->getTotalPages();
				$res['p'] = $Page->nowPage;
				$res['psize'] = $psize;
			}else{
				// 
				$res['is_picture'] = 2;
				$res['msg'] = '您的查询结果有误';
			}			
		}else{
			$res['data'] = '查询结果为空';
			$res['is_picture'] = 1;
		}
		e_json($res);      
	}

	// add multiple files
	public function uploadmultiplefiles(){
        if (IS_GET) {
            //
        }
        else{
            $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'Error');
            $fname = I('post.name','multiplefiles', 'trim');
            $newName = I('post.newname','','trim');
            $from = I('post.from','', 'trim');
            $Mdo = new \Common\ORG\AttUpload();
            if ( $_FILES && isset( $_FILES[$fname] ) ){
                $fupload = $_FILES[$fname];
                $is_multi = is_array($fupload['error'])  ? true : false;
                $res['f'] = $is_multi;
                if ( (!$is_multi&&$fupload['error'] != 0) || ($is_multi&&count($fupload['error'])==0)  ||
                         ($is_multi && $fupload['error'][0]!=0 )){
                    $res['msg'] = "上传文件发生错误";
                }
                else {
                    if ($is_multi) {  // 二维数组批量上传文件
                        $res = array('rcode'=>0,'data'=>array(),'msg'=>'Error');
                        $allFileSize = count($fupload['name']);
                        $countUploaded = 0;
                        for ($i=0;  $i<$allFileSize; $i++) {
                            $ret = $Mdo->upload( $fupload['name'][$i], $fupload['tmp_name'][$i], $newName);
                            if ($ret&&$ret['rcode']){
                                $countUploaded++;
                                $res['view_path'][] = $ret['data']['view_path'];
								$res['source_name'][]= $ret['data']['source_name'];
                            } 
                            
                        }
                        if ($countUploaded) {
                            $res['rcode'] = 1;
                            $res['msg'] = "OK";
                        }
                        else 
                            $res['msg'] = "批量上传文件失败";
                    }
                    else {
                        $ret = $Mdo->upload( $fupload['name'], $fupload['tmp_name'], $newName);
						$res['view_path'] = $ret['data']['view_path'];
						$res['source_name'] = $ret['data']['source_name'];
                    }
                }
            }
            else
                $res['msg'] = "获取上传文件失败";
            e_json( $res );
        }		
	}

 	//add knowledgeproperty
	public function AddKnowdedge(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$str = array();
		if($_POST['param']['abstract']){
			$wheres['abstract'] = $_POST['param']['abstract'];
		}else{
			if($_POST['param']['templete']){
				$str = explode('。',strip_tags($_POST['param']['templete']));
				$wheres['abstract'] = $str[0];
			}
		}
		$_POST['param']['title'] ?  $wheres['title'] = $_POST['param']['title'] : ''; 
		$_POST['param']['FirstImgPath'] ? $wheres['first_page_image'] = $_POST['param']['FirstImgPath'] : '';
		$_POST['param']['author'] ? $wheres['pen_name'] = $_POST['param']['author'] :'';
		$_POST['param']['templete'] ? $wheres['content'] = $_POST['param']['templete'] :'';
		$_POST['param']['url'] ? $wheres['artical_url'] = $_POST['param']['url'] : '';
		$_POST['param']['msg_type'] ? $wheres['msg_type'] = $_POST['param']['msg_type'] : '';
		$_POST['param']['checked'] == 1 ? $wheres['first_page_show'] = 1 : $wheres['first_page_show'] = 0;                       	
		$wheres['author_id'] = $this->m_user['user_id'];
		$wheres['knowledge_spot_id'] = $_POST['knowledge_spot_id'];
		$wheres['is_delete'] = 0;
		$knowledgeAdd = M('knowledge')->add($wheres);
		if($knowledgeAdd){
			if($_POST['view_path'] &&  $_POST['original_name']){
				foreach ($_POST['view_path'] as $k => $v) {
					$arr['knowledge_file_name'] = $v;
					$arr['knowledge_id'] = $knowledgeAdd;
					$arr['file_original_name'] = $_POST['original_name'][$k];
					$arr['is_delete'] = 0;
					$addknowledgefile = M('knowledge_file')->add($arr);
					if($addknowledgefile){
						$res['rcode'] = 1;
						$res['condition'] = $_POS['knowledge_spot_id'];
						$res['msg'] = 'OK';
					}else{
						$res['rcode'] = 0;
						$res['condition'] = $_POST['knowledge_spot_id'];
						$res['msg'] = '添加图文信息失败！';
						e_json($res);	
					}			
				}
			}else{
					$res['rcode'] = 1;
					$res['condition'] = $_POST['knowledge_spot_id'];				
			}
		}else{
			// 
			$res['data'] = '数据插入失败';
		}
		// $res['post'] = $_POST;
		e_json($res);
	} 

	// second tab  file upload 
	public function AddFileUpload(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$_POST['param']['filetitle'] ? $fileUpload['title'] = $_POST['param']['filetitle'] : '';
		$_POST['param']['msg_type'] ? $fileUpload['msg_type'] = $_POST['param']['msg_type'] : '';
		$_POST['knowledge_spot_id'] ? $fileUpload['knowledge_spot_id'] = $_POST['knowledge_spot_id'] :'';
		$callfun = M('user')->field('nickname')->where(array('user_id'=>$this->m_user['user_id']))->find();
		$callfun ? $fileUpload['pen_name'] = $callfun['nickname']:'';
		$_POST['param']['fileTrans'] ? $wheres['knowledge_file_name'] = $_POST['param']['fileTrans'] : '';
		$_POST['param']['original_name'] ? $wheres['file_original_name'] = $_POST['param']['original_name']:'';
		$wheres['is_delete'] = 0;
		$fileUpload['author_id'] = $this->m_user['user_id'];
		$fileUpload['is_delete'] = 0;
		$fileUploadAdd = M('knowledge')->add($fileUpload);
		if($fileUploadAdd){
			$wheres['knowledge_id'] = $fileUploadAdd;
			$fileAdd = M('knowledge_file')->add($wheres);
			if($fileAdd){
				$res['rcode'] = 1;
				$res['msg'] = 'OK';
				$res['condition'] = $_POST['knowledge_spot_id'];
			}else{
				$res['rcode'] = 0;
				$res['msg'] = '添加文件信息失败！';
				$res['condition'] = $_POST['knowledge_spot_id'];
				e_json($res);	
			}	
		}
		e_json($res);

	}

	// third tab url upload
	public function urlUpload(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$wheres['author_id'] = $this->m_user['user_id'];
		$_POST['param']['urltitle'] ? $wheres['title'] = $_POST['param']['urltitle'] : '';
		$_POST['param']['msg_type'] ? $wheres['msg_type'] = $_POST['param']['msg_type'] : '';
		$_POST['param']['outerUrl'] ? $wheres['artical_url'] = $_POST['param']['outerUrl'] : '';
		$_POST['param']['urlabstract'] ? $wheres['abstract'] = $_POST['param']['urlabstract'] : '';
		$_POST['knowledge_spot_id'] ? $wheres['knowledge_spot_id'] = $_POST['knowledge_spot_id'] : '';
		$_POST['param']['thirdtabchk'] == 1? $wheres['first_page_show'] = 1 : $wheres['first_page_show'] = 0; 
		$_POST['param']['urlfirstimg'] ? $wheres['first_page_image'] = $_POST['param']['urlfirstimg'] : '';
		$callfun = M('user')->field('nickname')->where(array('user_id'=>$this->m_user['user_id']))->find();
		$callfun ? $wheres['pen_name'] = $callfun['nickname']:'';
		$wheres['is_delete'] = 0;
		$urlUploadAdd = M('knowledge')->add($wheres);
		if($urlUploadAdd){
			$res['rcode'] = 1;
			$res['condition'] = $_POST['knowledge_spot_id'];
			$res['msg'] = 'OK';
		}else{
			$res['rcode'] = 0;
			$res['condition'] = $_POST['knowledge_spot_id'];
			$res['msg'] = '添加文件信息失败！';			
		}
		e_json($res);
	}

	// get tab data 
	public function GetTabData(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		// $display = I('post.display',false,'intval');
		$knowledge = I('post.knowledgeId',false,'intval');
		$dlist = D('Admin/Knowledge')
									->where(array('knowledge_id'=>$knowledge))
									->find();	
		// $dlist['path'] = $dlist['first_page_image'];
					$res['post'] = $_POST;
		if($dlist) {
			$res['knowledge'] = $knowledge;
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['sql'] = D('Admin/Knowledge')->getlastsql();
		}
		e_json($res);
	}
	// Get files of the first Tab 
	public function GetFirstTabFile(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');	
		$knowledge = I('post.knowledgeId',false,'intval');	
		$flist = M('knowledge_file')->where(array('knowledge_id'=>$knowledge))->select();	
		foreach($flist as $key => $val){
			$flist[$key]['name'] = $val['file_original_name'];
		}
		if($flist) {
			$res['rcode'] = 1;
			$res['data'] = $flist;
		}
		e_json($res);	
	}

	// edit editknowledgeproperty
	public function editknowledgeproperty(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$_POST['editparam'] ? $editparam = $_POST['editparam'] : '';
		$_POST['view_path'] ? $view_path = $_POST['view_path'] : '';
		$_POST['original_name'] ? $original_name = $_POST['original_name'] :'';
		$_POST['oldfile'] ? $oldfile = $_POST['oldfile'] :'';

		$delres = M('knowledge_file')->where(array('knowledge_id'=>$editparam['knowledge_id']))->delete();

		if($editparam['edituploadimg']){
			$wheres['first_page_image'] =  $editparam['edituploadimg'];
		}else{
			$wheres['first_page_image'] = '';
		}
		$wheres['title'] =  $editparam['title'];
		$wheres['abstract'] =  $editparam['abstract'];
		$wheres['first_page_show'] =  $editparam['editcheck'];
		$wheres['msg_type'] =  $editparam['msg_type'];
		$wheres['content'] =  $editparam['content'];
		$wheres['artical_url'] =  $editparam['artical_url'];
		$wheres['knowledge_spot_id'] =  $editparam['knowledge_spot_id'];
		$wheres['pen_name'] =  $editparam['pen_name'];
		$wheres['author_id'] =  $editparam['author_id'];
		$wheres['is_delete'] = 0;
		$knowledgeRes = D('Admin/knowledge')->where(array('knowledge_id'=> $editparam['knowledge_id']))->save($wheres);
		if($oldfile){
			foreach($oldfile as $key=>$val){
					$old['knowledge_id'] = $val['knowledge_id'];
					$old['knowledge_file_name'] = $val['knowledge_file_name'];
					$old['file_original_name'] = $val['file_original_name'];
					$old['is_delete'] = 0;
				$knowledgeAdd = M('knowledge_file')->add($old);
			}
		}
		if($original_name && $view_path){
			$num = 0;
			foreach($original_name as $key => $val ){
					$arr['knowledge_id'] = $editparam['knowledge_id'];
					$arr['knowledge_file_name'] = $view_path[$num];	
					$arr['file_original_name'] = $val;
					$arr['is_delete'] = 0;
					$num++;
				$knowledgeAdd = M('knowledge_file')->add($arr);
			}
		}else{
			// 
		}
		if($knowledgeRes || $knowledgeAdd ){
			$res['rcode'] = 1;
			$res['condition'] = $editparam['knowledge_spot_id'];
		}else{
			$res['condition'] = $editparam['knowledge_spot_id'];
		}
		$res['post'] = $_POST;
		e_json($res);
	}

	// get data of tab second
	public function GetSecondTabData(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$knowledge = I('post.knowledgeId',false,'intval');
		$dlist = D('Admin/Knowledge')
									->where(array('knowledge_id'=>$knowledge))
									->find();
		$dlist['file_name'] = M('knowledge_file')->field('file_original_name,knowledge_file_name')->where(array('knowledge_id'=>$knowledge))->find();
		$dlist['name'] = $dlist['file_name']['file_original_name'];
		$dlist['secondfilename'] = $dlist['file_name']['knowledge_file_name'];
		if($dlist) {
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['sql'] = D('Admin/Knowledge')->getlastsql();
		}
		e_json($res);		
	}

	// edit data of second tab
	public function EditSecondTab(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$_POST['title'] ? $arr['title'] = $_POST['title'] : '';
		$_POST['knowledge_id'] ? $arr['knowledge_id'] = $_POST['knowledge_id'] : '';
		// $_POST['knowledge_id'] ? $arr['knowledge_id'] = $_POST['knowledge_id'] : '';
		$_POST['knowledge_spot_id'] ? $arr['knowledge_spot_id'] = $_POST['knowledge_spot_id'] : '';
		$_POST['knowledge_id'] ? $wheres['knowledge_id'] = $_POST['knowledge_id'] : '';
		$_POST['filename'] ? $wheres['file_original_name'] = $_POST['filename'] : '';
		$_POST['edituploadfile'] ? $wheres['knowledge_file_name'] = $_POST['edituploadfile'] : $wheres['knowledge_file_name'] = '';
		$wheres['is_delete'] = 0;
		$SecondTabKnowledge = D('Admin/knowledge') -> save($arr);
		$SecondTabKnowledgeFileDel = M('knowledge_file')->where(array('knowledge_id'=>$_POST['knowledge_id']))->delete();
		$SecondTabKnowledgeFile = M('knowledge_file')->add($wheres);
		if($SecondTabKnowledge || $SecondTabKnowledgeFile){
			$res['rcode'] = 1;
			$res['condition'] = $_POST['knowledge_spot_id'];	
			
		}else{
			$res['condition'] = $_POST['knowledge_spot_id'];
		}
		$res['post'] = $_POST;
		e_json($res);
	}

	// get data of third tab
	public function GetThirdTabData(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$knowledge = I('post.knowledgeId',false,'intval');
		$dlist = D('Admin/Knowledge')
									->where(array('knowledge_id'=>$knowledge))
									->find();
		if($dlist) {
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['sql'] = D('Admin/Knowledge')->getlastsql();
		}
		e_json($res);			
	}

	// edit third tab 
	public function EditThirdTab(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$_POST['title'] ? $arr['title'] = $_POST['title'] : '';
		// $_POST['knowledge_id'] ? $arr['knowledge_id'] = $_POST['knowledge_id'] : '';
		$_POST['abstract'] ? $arr['abstract'] = $_POST['abstract'] : '';
		$_POST['urlexpression'] == 1 ? $arr['first_page_show'] = 1 : $arr['first_page_show'] = 0;
		$_POST['artical_url'] ? $arr['artical_url'] = $_POST['artical_url'] : '';
		if($_POST['edituploadimg']){
			$arr['first_page_image'] = $_POST['edituploadimg'];
		}else{
			$arr['first_page_image'] = '';
		}	
		$ThirdTabKnowledge = M('knowledge')->where(array('knowledge_id'=>$_POST['knowledge_id']))->save($arr);
		if($ThirdTabKnowledge){
			$res['rcode'] = 1;	
			$res['conditon'] = $_POST['knowledge_spot_id'];
		}else{
			$res['condition'] = $_POST['knowledge_spot_id'];
		}
		$res['post'] = $_POST;
		e_json($res);		
	}

	// del knowledge title record
	public function delknowledgerecord(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');	
		$knowledge_id = I('post.knowledgeId',false,'intval');	
		$sign = I('post.sign',false,'intval');
		$kn = I('post.kn',false,'intval');
		if($sign == 1){
			$delfirsttab = D('Admin/knowledge')->where(array('knowledge_id'=>$knowledge_id))->save(array('is_delete'=>1));
			$delfirsttabfile = M('knowledge_file')->where(array('knowledge_id'=>$knowledge_id))->save(array('is_delete'=>1));
		}else if( $sign == 2){
			$delsecondtab = D('Admin/knowledge')->where(array('knowledge_id'=>$knowledge_id))->save(array('is_delete'=>1));
			$delsecondtabfile = M('knowledge_file')->where(array('knowledge_id'=>$knowledge_id))->save(array('is_delete'=>1));
		}else{
			$delthirdtab = D('Admin/knowledge')->where(array('knowledge_id'=>$knowledge_id))->save(array('is_delete'=>1));
		}
		$res['condition'] = $kn;
		e_json($res);
	}

	// get knowledgeright data
	public function GetKnowledgeData(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$arr = $sec = $user = array();
		if($this->m_is_admin){
			$callfun = M('knowledge_spot')->where(array('is_delete'=>array('neq',1)))->order('code asc')->select();
		}else{
			$data = M('sector')->select();
			$callfunsec = GetParentSector($data,$this->m_user['sector_id']);
			$callfunall = M('knowledge_spot_sector_right')->distinct(true)->field('knowledge_spot_id')->where(array('sector_id'=>array('IN',$callfunsec)))->select();
			if($callfunall){
				foreach($callfunall  as $key => $val){
					$sec[] = $val['knowledge_spot_id'];
				}				
			}
			$callfunuser = M('knowledge_spot_user_right')->distinct(true)->field('knowledge_spot_id')->where(array('user_id'=>$this->m_user['user_id']))->select();
			if($callfunuser){
				foreach($callfunuser as $kuser =>$vuser){
					$user[] = $vuser['knowledge_spot_id'];
				}
			}

			if($sec || $user){              
				$callfun = M('knowledge_spot')-> where(array('knowledge_spot_id'=>array('IN',array_unique(array_merge($sec,$user))),'is_delete'=>array('neq',1)))->order('code asc')->select();
			}
		}
        if($callfun){
            foreach($callfun as $key => $val){
                if(strlen($val['code']) == 2){
					$callfun[$key]['disabled'] = 1;
                }else{
					$callfun[$key]['disabled'] = 2;
                }
            }
            if($callfun){
                $res['rcode'] = 1;
                $res['data'] = $callfun;
            }            
        } 
        e_json($res);	
	}

	// show the path of knowledge node
	public function ShowNode(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$knowledge_spot_id = I('post.knowledge_spot_id',false,'intval');
		$user_id = $this->m_user['user_id'];
		$sector_id = $this->m_user['sector_id'];
		$this->m_is_admin ? $right = 1 : $right = 0;
		$code = GetNodePath($knowledge_spot_id,$user_id,$sector_id,$right);
		if($code){
			$res['rcode'] = 1;
			$res['data'] = $code;
		}
		e_json($res);
	}

	// view knowledge msg
	public function ViewKnowledgeMsg(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$knowledge_id = I('post.knowledgeId',false,'intval');
		if($knowledge_id){
			$callfun = D('Admin/knowledge')->where(array('knowledge_id'=>$knowledge_id))->find();
		}
		if($callfun){
			$res['rcode'] = 1;
			$res['data'] = $callfun;
		}
		$res['post'] = $callfun;
		e_json($res);
	}

    // get userId
    public function LoginUserId(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
        if($this->m_user['user_id']){
            $res['rcode'] = 1;
            $res['data'] = $this->m_user['user_id'];
         }
        e_json($res);
    }

	// download second tab file
	public function DownSecondTabFile(){
		$res = array('rcode'=>0,'data'=>'','msg'=>'');
		$knowledge_file_id = I('get.knowledgefileId',false,'intval');
		DownLoadFile($knowledge_file_id);
	}

	// get file name and extension name
	public function GetFileName(){
		$res = array('rcode'=>0,'data'=>'','msg'=>'');
		$knowledge_file_id = I('get.knowledgefileId',false,'intval');
		$callfun = GetFName($knowledge_file_id);
		if($callfun){
			$res['rcode'] = 1;
			$res['extersion'] = $callfun['extersion'];
			$res['name'] =$callfun['name'];			
		}
		e_json($res);
	}
}
?>